/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package SharedDrawing;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JPanel;
import common.XmlConstants;

public class GraphicObject 
{
	private long m_id, m_oldId;
	private long m_objLevel;
	private static long m_levels;
	private BoundingBox m_currentState;
    private ArrayList<BoundingBox> m_previousStates, m_nextStates;
    
    public GraphicObject(BoundingBox currentState)
    {
    	m_currentState = currentState;
    	
    	m_levels++;
		m_objLevel = m_levels;
		
    	m_id = -1;
    	m_oldId = Pages.getInstance().getNextId();
    	
    	m_previousStates = new ArrayList<BoundingBox>();
    	m_nextStates = new ArrayList<BoundingBox>();
    }
    
    public void archiveState(JPanel container)
    {
    	try 
    	{
	    	ShapeContent currentShape = toShapeContent();
	    	
	    	BoundingBox newObj = null;
	    	
	    	newObj = Pages.getInstance().getFactory().getInstance(currentShape.getBoundingBox().getType());
			
	    	 if(newObj == null)
             {
                 throw new Exception("Factory problem");
             }
             else
             {
            	 newObj.initShape(container, 0, 0, 0, true, Color.black, Color.black);
                 newObj.fromShapeContent(currentShape); 
            	 m_previousStates.add(newObj);
             }
	    	
		} 
    	catch (InstantiationException e) {
			e.printStackTrace();
		} 
    	catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
    }
	
    public BoundingBox getArchiveState(JPanel container)
    {
    	try 
    	{
	    	ShapeContent currentShape = toShapeContent();
	    	
	    	BoundingBox newObj = null;
	    	
	    	newObj = Pages.getInstance().getFactory().getInstance(currentShape.getBoundingBox().getType());
			
	    	 if(newObj == null)
             {
                 throw new Exception("Factory problem");
             }
             else
             {
            	 newObj.initShape(container, 0, 0, 0, true, Color.black, Color.black);
                 newObj.fromShapeContent(currentShape); 
            	 return newObj;
             }
	    	
		} 
    	catch (InstantiationException e) {
			e.printStackTrace();
		} 
    	catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }
	 /**
	 * Function used to transform the current object in a ShapeContent 
	 * @return the ShapeContent with the values depending of the object
	 */
	protected ShapeContent toShapeContent()
	{
		ShapeContent shape = m_currentState.toShapeContent();
		
        WgContent wg = shape.getBoundingBox();
        
        wg.addContent(XmlConstants.shapeContentId, String.valueOf(m_id));
        wg.addContent(XmlConstants.shapeContentOldId, String.valueOf(m_oldId));
        wg.addContent(XmlConstants.shapeContentLevel, String.valueOf(m_objLevel));
        
        shape.setBoundingBox(wg);
        
        return shape;
    }
	
	/**
	 * Function used to set the attributes of all the elements of the object from a ShapeContent
	 */
	protected void fromShapeContent(ShapeContent content)
    {
        WgContent wg = content.getBoundingBox();
        String s_id = wg.get(XmlConstants.shapeContentId);
        
        if(s_id == null){
            m_id = -1;
        }
        else{
            m_id = Integer.parseInt(wg.get(XmlConstants.shapeContentId));
        }
        
        m_oldId = Integer.parseInt(wg.get(XmlConstants.shapeContentOldId));
        m_objLevel = Integer.parseInt(wg.get(XmlConstants.shapeContentLevel)); 
        
        m_currentState.fromShapeContent(content);
    }
	
	public void setObjectTop() {
		m_levels++;
		m_objLevel = m_levels;
	}
	
	public long getObjLevel() {
		return m_objLevel;
	}
	
	public BoundingBox getCurrentState() {
		return m_currentState;
	}

	public void setCurrentState(BoundingBox m_currentState) {
		this.m_currentState = m_currentState;
	}

	public ArrayList<BoundingBox> getPreviousStates() {
		return m_previousStates;
	}

	public ArrayList<BoundingBox> getNextStates() {
		return m_nextStates;
	}

	public long getId() {
		return m_id;
	}

	public void setId(long m_id) {
		this.m_id = m_id;
	}

	public long getOldId() {
		return m_oldId;
	}

	public void setOldId(long m_oldId) {
		this.m_oldId = m_oldId;
	}
}
